home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-12-16 | 18.9 KB | 649 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWColor.cpp
- // Release Version: $ ODF 3 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWCOLOR_H
- #include "FWColor.h"
- #endif
-
- #ifndef FWSTDDEF_H
- #include "FWStdDef.h"
- #endif
-
- #ifndef FWSTRMRW_H
- #include "FWStrmRW.h"
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwgraphx1
- #endif
-
- //========================================================================================
- // struct FW_CColor
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::FW_CColor
- //----------------------------------------------------------------------------------------
-
- FW_CColor::FW_CColor()
- {
- fRGB = 0; // Black
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::FW_CColor
- //----------------------------------------------------------------------------------------
-
- FW_CColor::FW_CColor(FW_RGBComponent r, FW_RGBComponent g, FW_RGBComponent b)
- {
- SetRGB(r, g, b);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::FW_CColor
- //----------------------------------------------------------------------------------------
-
- FW_CColor::FW_CColor(const FW_CColor& colorFrom, const FW_CColor& colorTo, unsigned short numer, unsigned short denom)
- {
- Blend(colorFrom, colorTo, numer, denom);
- }
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CColor::FW_CColor
- //----------------------------------------------------------------------------------------
-
- FW_CColor::FW_CColor(const RGBColor* rgbPtr)
- {
- *this = *rgbPtr;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CColor::FW_CColor
- //----------------------------------------------------------------------------------------
-
- FW_CColor::FW_CColor(const RGBColor& rgb)
- {
- *this = rgb;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- //----------------------------------------------------------------------------------------
- // FW_CColor::FW_CColor
- //----------------------------------------------------------------------------------------
-
- FW_CColor::FW_CColor(COLORREF colorref)
- {
- fRGB = colorref;
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::FW_CColor
- //----------------------------------------------------------------------------------------
-
- FW_CColor::FW_CColor(FW_SColor sColor)
- {
- fRGB = sColor.fRGB;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::FW_CColor
- //----------------------------------------------------------------------------------------
-
- FW_CColor::FW_CColor(const FW_CColor& color)
- {
- fRGB = color.fRGB;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPoint::FW_CPoint
- //----------------------------------------------------------------------------------------
-
- FW_CColor::FW_CColor(FW_CReadableStream& stream)
- {
- Read(stream);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator=(FW_SColor sColor)
- {
- fRGB = sColor.fRGB;
- return (*this);
- }
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator==
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CColor::operator==(const RGBColor& rgb) const
- {
- FW_SColor color;
- FW_PrivSetRGB(color, rgb.red >> 8, rgb.green >> 8, rgb.blue >> 8);
- return color.fRGB == fRGB;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator=(const RGBColor& rgb)
- {
- SetRGB(rgb.red >> 8, rgb.green >> 8, rgb.blue >> 8);
- return *this;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CColor::operator RGBColor() const
- {
- RGBColor rgb;
-
- rgb.red = FW_PrivRGB_R(*this);
- rgb.red |= rgb.red << 8;
-
- rgb.green = FW_PrivRGB_G(*this);
- rgb.green |= rgb.green << 8;
-
- rgb.blue = FW_PrivRGB_B(*this);
- rgb.blue |= rgb.blue << 8;
-
- return rgb;
- }
-
- #endif
-
- #ifdef FW_BUILD_WIN
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator=(RGBQUAD quad)
- {
- SetRGB(quad.rgbRed, quad.rgbGreen, quad.rgbBlue);
-
- return *this;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CColor::operator RGBQUAD() const
- {
- unsigned short r, g, b;
- RGBQUAD quad;
-
- GetRGB(r, g, b);
-
- quad.rgbRed = (FW_RGBComponent)r;
- quad.rgbGreen = (FW_RGBComponent)g;
- quad.rgbBlue = (FW_RGBComponent)b;
- quad.rgbReserved = 0;
-
- return quad;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator=(RGBTRIPLE triple)
- {
- SetRGB(triple.rgbtRed, triple.rgbtGreen, triple.rgbtBlue);
-
- return *this;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CColor::operator RGBTRIPLE() const
- {
- unsigned short r, g, b;
- RGBTRIPLE triple;
-
- GetRGB(r, g, b);
-
- triple.rgbtRed = (FW_RGBComponent)r;
- triple.rgbtGreen = (FW_RGBComponent)g;
- triple.rgbtBlue = (FW_RGBComponent)b;
-
- return triple;
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator=(const FW_CColor& color)
- {
- fRGB = color.fRGB;
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // Arithmetic, by another color for FW_CColor
- // Only + and - really make sense for FW_CColor operands, but multiply and divide
- // exist for consistency, since they do make sense for scalars
- //----------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::AddComponents
- //----------------------------------------------------------------------------------------
-
- inline
- FW_RGBComponent AddComponents(unsigned short a, unsigned short b)
- {
- a += b;
-
- if (a > 255)
- a = 255;
-
- return (FW_RGBComponent)a;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::SubtractComponents
- //----------------------------------------------------------------------------------------
-
- inline
- FW_RGBComponent SubtractComponents(unsigned short a, unsigned short b)
- {
- return (FW_RGBComponent)(a <= b ? 0 : a - b);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::MultiplyComponents
- //----------------------------------------------------------------------------------------
-
- inline
- FW_RGBComponent MultiplyComponents(unsigned short a, unsigned short b)
- {
- a *= b;
-
- if (a > 255)
- a = 255;
-
- return (FW_RGBComponent)a;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::DivideComponents
- //----------------------------------------------------------------------------------------
-
- inline
- FW_RGBComponent DivideComponents(unsigned short a, unsigned short b)
- {
- return (FW_RGBComponent)(b == 0 ? 255 : a / b);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::AverageComponents
- //----------------------------------------------------------------------------------------
-
- inline
- FW_RGBComponent AverageComponents(unsigned short a, unsigned short b)
- {
- return (FW_RGBComponent)((a + b) / 2);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::BlendComponents
- //----------------------------------------------------------------------------------------
-
- inline
- FW_RGBComponent BlendComponents(unsigned short a, unsigned short b, unsigned short numer, unsigned short denom)
- {
- return (FW_RGBComponent)(a + ((((long) b - a) * numer) / denom));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator+
- //----------------------------------------------------------------------------------------
-
- FW_CColor FW_CColor::operator+(const FW_CColor& color) const
- {
- FW_CColor newColor(*this);
- return newColor += color;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator-
- //----------------------------------------------------------------------------------------
-
- FW_CColor FW_CColor::operator -(const FW_CColor& color) const
- {
- FW_CColor newColor(*this);
- return newColor -= color;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator +=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator+=(const FW_CColor& color)
- {
- unsigned short red1, green1, blue1, red2, green2, blue2;
-
- GetRGB(red1, green1, blue1);
- color.GetRGB(red2, green2, blue2);
-
- SetRGB(AddComponents(red1, red2), AddComponents(green1, green2), AddComponents(blue1, blue2));
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator -=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator-=(const FW_CColor& color)
- {
- unsigned short red1, green1, blue1, red2, green2, blue2;
-
- GetRGB(red1, green1, blue1);
- color.GetRGB(red2, green2, blue2);
-
- SetRGB(SubtractComponents(red1, red2), SubtractComponents(green1, green2), SubtractComponents(blue1, blue2));
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator +
- //----------------------------------------------------------------------------------------
-
- FW_CColor FW_CColor::operator+(unsigned short aScalar) const
- {
- FW_CColor newColor(*this);
-
- return (newColor += aScalar);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator -
- //----------------------------------------------------------------------------------------
-
- FW_CColor FW_CColor::operator-(unsigned short aScalar) const
- {
- FW_CColor newColor(*this);
-
- return (newColor -= aScalar);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator *
- //----------------------------------------------------------------------------------------
-
- FW_CColor FW_CColor::operator*(unsigned short aScalar) const
- {
- FW_CColor newColor(*this);
-
- return (newColor *= aScalar);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator /
- //----------------------------------------------------------------------------------------
-
- FW_CColor FW_CColor::operator/(unsigned short aScalar) const
- {
- FW_CColor newColor(*this);
-
- return (newColor /= aScalar);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator +=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator+=(unsigned short aScalar)
- {
- unsigned short red, green, blue;
-
- GetRGB(red, green, blue);
-
- SetRGB(AddComponents(red, aScalar),
- AddComponents(green, aScalar),
- AddComponents(blue, aScalar));
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator -=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator-=(unsigned short aScalar)
- {
- unsigned short red, green, blue;
-
- GetRGB(red, green, blue);
-
- SetRGB(SubtractComponents(red, aScalar),
- SubtractComponents(green, aScalar),
- SubtractComponents(blue, aScalar));
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator *=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator*=(unsigned short aScalar)
- {
- unsigned short red, green, blue;
-
- GetRGB(red, green, blue);
-
- SetRGB(
- MultiplyComponents(red, aScalar),
- MultiplyComponents(green, aScalar),
- MultiplyComponents(blue, aScalar)
- );
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::operator /=
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::operator/=(unsigned short aScalar)
- {
- unsigned short red, green, blue;
-
- GetRGB(red, green, blue);
-
- SetRGB(
- DivideComponents(red, aScalar),
- DivideComponents(green, aScalar),
- DivideComponents(blue, aScalar)
- );
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::Brightness
- //----------------------------------------------------------------------------------------
-
- unsigned long FW_CColor::Brightness() const
- {
- unsigned short red, green, blue;
-
- GetRGB(red, green, blue);
-
- // calculate approximate brightness using YIQ formula where
- // Y = lumenance = .30R + .59G + .11B
- // approx. fY := (r / 4) + 5 * (g / 8) + b / 8 ≈ (.25 R + .625 G + .125 B)
-
- return (red >> 2) + (5L * (green >> 3)) + (blue >> 3);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::IsDarkerThan
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CColor::IsDarkerThan(const FW_CColor& color) const
- {
- return Brightness() < color.Brightness();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::IsLighterThan
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CColor::IsLighterThan(const FW_CColor& color) const
- {
- return Brightness() > color.Brightness();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::Average
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::Average(const FW_CColor& colorA, const FW_CColor& colorB)
- {
- unsigned short redA, greenA, blueA;
- unsigned short redB, greenB, blueB;
-
- colorA.GetRGB(redA, greenA, blueA);
- colorB.GetRGB(redB, greenB, blueB);
-
- SetRGB(AverageComponents(redA, redB),
- AverageComponents(greenA, greenB),
- AverageComponents(blueA, blueB));
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::Average
- //----------------------------------------------------------------------------------------
-
- FW_CColor& FW_CColor::Blend(const FW_CColor& colorFrom, const FW_CColor& colorTo, unsigned short numer, unsigned short denom)
- {
- unsigned short redA, greenA, blueA;
- unsigned short redB, greenB, blueB;
-
- colorFrom.GetRGB(redA, greenA, blueA);
- colorTo.GetRGB(redB, greenB, blueB);
-
- if (numer == 0)
- {
- SetRGB((FW_RGBComponent)redA, (FW_RGBComponent)greenA, (FW_RGBComponent)blueA);
- }
- else if (numer >= denom || denom == 0)
- {
- SetRGB((FW_RGBComponent)redB, (FW_RGBComponent)greenB, (FW_RGBComponent)blueB);
- }
- else
- {
- SetRGB(
- BlendComponents(redA, redB, numer, denom),
- BlendComponents(greenA, greenB, numer, denom),
- BlendComponents(blueA, blueB, numer, denom));
- }
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::SetRGB
- //----------------------------------------------------------------------------------------
-
- void FW_CColor::SetRGB(FW_RGBComponent r, FW_RGBComponent g, FW_RGBComponent b)
- {
- FW_PrivSetRGB(*this, r, g, b);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::GetRGB
- //----------------------------------------------------------------------------------------
-
- void FW_CColor::GetRGB(unsigned short& r, unsigned short& g, unsigned short& b) const
- {
- r = FW_PrivRGB_R(*this);
- g = FW_PrivRGB_G(*this);
- b = FW_PrivRGB_B(*this);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::Read
- //----------------------------------------------------------------------------------------
-
- void FW_CColor::Read(FW_CReadableStream& stream)
- {
- unsigned short rgb[3];
- stream.Read(rgb, 3);
- SetRGB((FW_RGBComponent)rgb[0], (FW_RGBComponent)rgb[1], (FW_RGBComponent)rgb[2]);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CColor::Write
- //----------------------------------------------------------------------------------------
-
- void FW_CColor::Write(FW_CWritableStream& stream) const
- {
- unsigned short rgb[3];
- GetRGB(rgb[0], rgb[1], rgb[2]);
- stream.Write(rgb, 3);
- }
-
- //----------------------------------------------------------------------------------------
- // operator>>
- //----------------------------------------------------------------------------------------
-
- FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_CColor& color)
- {
- color.Read(stream);
- return stream;
- }
-
- //----------------------------------------------------------------------------------------
- // operator<<
- //----------------------------------------------------------------------------------------
-
- FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_CColor& color)
- {
- color.Write(stream);
- return stream;
- }
-
-